package net.neimeng.luculent.coremodel.base.view;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.igexin.sdk.PushManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.tapadoo.alerter.Alerter;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import net.neimeng.luculent.coremodel.R;
import net.neimeng.luculent.coremodel.datamodel.http.PageResult;
import net.neimeng.luculent.coremodel.utils.Logger;
import net.neimeng.luculent.dependencies.getui.GeTuiIntentService;
import net.neimeng.luculent.dependencies.getui.GeTuiPushService;

/**
 * Created by jingwenguang on 2018/1/17.
 */

public class BaseActivity extends AppCompatActivity {
    private  String TAG = "BaseActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //设置竖屏显示（禁止横屏）
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);
        // Activity 初始化性能优化 https://blog.csdn.net/u014700919/article/details/51178877
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                //初始化个推SDK
                PushManager.getInstance().initialize(BaseActivity.this, GeTuiPushService.class);
                PushManager.getInstance().registerPushIntentService(BaseActivity.this, GeTuiIntentService.class);
                onInit();
                return false; //false 表示只监听一次IDLE事件,之后就不会再执行这个函数了.
            }
        });
        Log.d(TAG,getClass().getSimpleName());
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        initToolbar();
    }

    //子类重写此函数即可,而不需要在onCreate()中去初始化.
    protected void onInit() {
        Logger.d(getClass().getSimpleName(), "执行了=========onInit");
    }

    private void initToolbar(){
        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView centerTitle = findViewById(R.id.centerTitle);
        if (toolbar != null) {
            toolbar.setTitle("");
            centerTitle.setText("");
            setSupportActionBar(toolbar);
        }
    }

    protected void hideBackNavigation(){
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            getSupportActionBar().setHomeButtonEnabled(false);
        }
    }

    protected void showBackNavigation(){
        if(getSupportActionBar() != null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        TextView centerTitle = findViewById(R.id.centerTitle);
        if (centerTitle != null) {
            centerTitle.setText(title);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void showError(TextInputLayout textInputLayout, String error) {
        textInputLayout.setError(error);
        textInputLayout.getEditText().setFocusable(true);
        textInputLayout.getEditText().setFocusableInTouchMode(true);
        textInputLayout.getEditText().requestFocus();
    }

    public void showError(EditText editText, String error) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        Alerter.create(this).setTitle(error).show();
    }

    public void showError(String error) {
        Alerter.create(this).setTitle(error).show();
    }

//    public void showHint(String message) {
//        Alerter.create(this).setBackgroundColorInt(ContextCompat.getColor(this,)).setTitle(message).show();
//    }

    //上拉刷新或者下拉加载后的统一处理方法  （暂时先放在这里，后期可以独立到一个类中）
    public <T> void afterRefreshLoadMore(SmartRefreshLayout refreshLayout, PageResult<T> result, CommonAdapter<T> adapter, EmptyWrapper emptyWrapper){
        switch (result.getOperateType()){
            case First:
                refreshLayout.setNoMoreData(!result.isHasNext());
                break;
            case Refresh:
                refreshLayout.finishRefresh();
                refreshLayout.setNoMoreData(!result.isHasNext());
                adapter.getDatas().clear();
                break;
            case LoadMore:
                if(!result.isHasNext()){
                    refreshLayout.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                }else{
                    refreshLayout.finishLoadMore();
                }
                break;
        }
        adapter.getDatas().addAll(result.getListData());
        emptyWrapper.notifyDataSetChanged();
    }

}
